<?php
include_once 'BaseDAO.php';
//include_once '../util/plog/classes/plog.php';

class UserDAO extends BaseDAO {
	
	public function __construct () {
		parent::__construct();
//		$this->log = Plog::factory(__FILE__);			
	}
	
	public function updateUserNotify(
					   $account,
					   $notifyBudgetUpdate,
					   $notifyAdApprovedNot,
					   $notifyAdModify,
					   $notifyAdApproved,
					   $notifyPayComplete) {
		   	
		  $sql =  "update user set  
		  			 notifyBudgetUpdate=$notifyBudgetUpdate,
		  			 notifyAdApprovedNot=$notifyAdApprovedNot,
		  			 notifyAdModify=$notifyAdModify,
		  			 notifyAdApproved=$notifyAdApproved,
		  			 notifyPayComplete=$notifyPayComplete 
		  			 where account = '$account'";
		  
		 $this->log->debug("更新使用者設定:".$sql);
		 $result = mysql_query($sql); 			
	 	 $this->log->debug("更新使用者設定結果:".$result);
		 return $result;
	}
	
	public function getManagerUser($account, $password) {
		
	}
	
	public function getUser($account, $password) {
		$sql = "select * from user  where  account = '$account' and password = '$password' and status=1";
		$result = @mysql_query($sql);
		$row = @mysql_fetch_object($result);
		return $row;
	}
	
	public function getMenagerUserAccount() {
		$sql = "select account from user  where  status=1 and type=0";
		$result = @mysql_query($sql);
		
		while ($row = @mysql_fetch_object($result)) {
			$row_set[] = $row->account;
		} 
		
		return $row_set;
	}
	
	
	public function getSettilDetailByAccount($account) {
		$sql = "select count(count) count, max(createtime) as createtime from settledetail where account = '$account'";
		$result = @mysql_query($sql);
		$row = @mysql_fetch_object($result);
		return $row;
	}
	
	 
	public function queryTargetAccountMember($account , $start , $limit) {
		$countSql = "select count(u.account) from user u where u.proxyAccount ='$account'";
		$countResult = @mysql_query($countSql);
		$count = mysql_fetch_row($countResult);
		
		$sql = "select u.account, u.name, u.status, u.createTime, 
				(select count(s.ChkSettle)  from settledetail s where s.account = u.account and s.ChkSettle=0 limit 1)  isPay,
				(select count(a.IsNew) from ad a where a.account = u.account and a.isNew =0 limit 1 ) isNewAd
				from user u where u.proxyAccount ='$account' limit $start , $limit";
		$result = @mysql_query($sql);
		$items = $this->toArray($result);
		
		return  array(
			"items" => $items,
			"count" => $count[0]
		);
	}
	
	public function update($user) {
		
	}
	
	public function getTargetAccount() {
		$sql = "select account ,companyName  from user where type=1 and status=1";
		$result = @mysql_query($sql);
		return $this->toArray($result);
	}
	
	public function checkAccount($account) {
		$sql = "select count(*) from user where account= '$account'";
		$result =  @mysql_query($sql);
		$row = @mysql_fetch_row($result);
		return $row[0];
	}
	
	public function insert($user) {		
		$sql = $this->genInserSQL("user", $user);	 	
	 	$this->log->debug("新增使用者 :".$sql);
	 	$result = @mysql_query($sql);	 	
	 	return $result;
	}
}

?>